package com.shijimo.business.controller;

import com.shijimo.server.dto.ResponseDto;
import com.shijimo.server.exception.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Dream_飞翔
 * @date 2021/10/9
 * @time 15:31
 * @email 1072876976@qq.com
 *
 * 自定义处理异常的类
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler(value = ValidationException.class)
    @ResponseBody
    public ResponseDto validatorExceptionHandler(ValidationException e) {
        ResponseDto responseDto = new ResponseDto();
        // 设置返回值为false
        responseDto.setSuccess(false);
        // 打印log日志输出
        LOG.warn(e.getMessage());

        responseDto.setMessage("请求参数异常！");
        return responseDto;
    }
}
